package com.jiufen.Controller;

import com.jiufen.Pojo.Emp;
import com.jiufen.Pojo.EmpQueryParam;
import com.jiufen.Pojo.PageResult;
import com.jiufen.Pojo.Result;
import com.jiufen.Service.EmpService;
import com.jiufen.anno.LogOperate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private  EmpService empService;
   /* @GetMapping
    public Result getEmpList(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                              String name,
                              Integer gender,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("查询员工列表--分页-参数{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult<Emp> pageResult = empService.getEmpListPage(page, pageSize,name,gender,begin,end);
        return Result.success(pageResult);
    }*/


    /**
     * 员工信息分页查询
     * @param empQueryParam
     * @return
     */
    @GetMapping
    public Result getEmpList(EmpQueryParam empQueryParam) {
        log.info("查询员工列表--分页带条件-参数{}",empQueryParam );
        PageResult<Emp> pageResult = empService.getEmpListPage(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 员工信息保存
     * @param emp
     * @return
     * @throws Exception
     */
    @LogOperate
    @PostMapping
    public Result saveEmp(@RequestBody Emp emp) throws Exception{
        //保存员工信息
        log.info("开始进行员工信息保存=={}",emp);
        Integer i = empService.saveEmp(emp);
        log.debug("save 成功的数量的行数：{}",i);
        return Result.success();
    }

    /**
     * 员工信息删除
     * @param ids
     * @return
     */
    @LogOperate
    @DeleteMapping
    //public Result deleteEmp(Integer[] ids) {
    public Result deleteEmp(@RequestParam List<Integer> ids) {
        log.info("删除员工信息--参数{}", ids);
        Integer i = empService.deleteEmp(ids);
        log.debug("共计删除数据总条数：{}条", i);
        return Result.success();
    }

    /**
     * 根据ID查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id) {
        log.info("根据id查询员工信息--参数{}", id);
        Emp emp = empService.getEmpById(id);
        log.debug("查询员工信息成功--{}", emp);
        return Result.success(emp);
    }

    /**
     * 修改员工数据
     */
    @LogOperate
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("修改员工信息--参数{}", emp);
        Integer i = empService.updateEmp(emp);
        log.debug("修改员工信息成功--{}", i);
        return Result.success();
    }


    /**
     * 请求路径：/emps/list
     * 请求方式：GET
     * 接口描述：该接口用于查询全部员工信息
     */
    @GetMapping("/list")
    public Result getEmpList() {
        log.info("查询全部员工信息无参数");
        List<Emp> empList = empService.getEmpListNew();
        return Result.success(empList);
    }
}
